// ! WordPress Mobi Reader  by  regou.me//

wpmr_i=0;
wpmr_n=0;
wpmr_loaded=0
function writedown(xml){       
        var wp_string="";
        var wpmr_title,wpmr_pubDate,wpmr_comment_no,wpmr_creator; 
        
       window.x=xml.getElementsByTagName("item");
        for(;wpmr_i<x.length && wpmr_i<WpMobiReader_itemsperpage*(wpmr_n+1);wpmr_i++){
            try{
            wpmr_title=x[wpmr_i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
            wpmr_title=del_bracket(wpmr_title);            
            wpmr_pubDate=x[wpmr_i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue.substring(0,16);
            wpmr_creator=x[wpmr_i].getElementsByTagNameNS('*','creator')[0].childNodes[0].nodeValue;
            wpmr_comment_no=x[wpmr_i].getElementsByTagNameNS('http://purl.org/rss/1.0/modules/slash/','comments')[0].childNodes[0].nodeValue;
                        
            
            wp_string+='<div data-role="collapsible" ><h3>'+wpmr_title+'</h3><div><ul data-role="listview" data-mini="true"><li><a href="#wpmr_fullview" class="fullview_click" wpmr_viewnum="'+wpmr_i+'">'+WpMobiReader_fullview+'...</a></li><li>'+WpMobiReader_author+'：'+wpmr_creator+'</li><li>'+wpmr_comment_no+WpMobiReader_comnum+'</li><li>'+wpmr_pubDate+'</li></ul></div></div>';
            
            }catch(e){alert(e)}  
        }
    
        var ori_string='<div data-role="collapsible-set" data-iconpos="right" data-theme="'+WpMobiReader_theme+'" >';
        $("#wpmr_itemlist").html(ori_string+wp_string+'</div>').trigger('create');
    
    }

 function del_bracket(str){ 
        var strback=str;
        if(str.indexOf(']')>0){
            var new_str=str.substring(str.indexOf(']')+1,str.length);  
            return new_str;
        }else{
            return strback
            }}


$(document).bind('pageinit',function(){      //All Start Here
    $("header").attr("data-theme",WpMobiReader_theme);
    
  if(!wpmr_loaded){
 $.ajax({ 
url:WpMobiReader_rssurl, 
dataType: 'xml',
error: function(data){ 
      alert('Error loading XML document'+datal); 
}, 
beforeSend: function(){  //issue :  Loading animate never hide
//$.mobile.pageLoading();
},
success: function(data){
    window.xml=data;
    wpmr_loaded=1;
        writedown(xml);       
},
complete: function(){
   // $.mobile.pageLoading(true); 
}
});
  }

$(".fullview_click").live("click",function(){  //点了看全文
var viewnum=$(this).attr("wpmr_viewnum");
var wpmr_subtitle=del_bracket(window.x[viewnum].getElementsByTagName("title")[0].childNodes[0].nodeValue);
var wpmr_full=window.x[viewnum].getElementsByTagNameNS('*','encoded')[0].childNodes[0].nodeValue;
wpmr_full='<h3>'+wpmr_subtitle+'</h3>'+wpmr_full;
var wpmr_gocomments=window.x[viewnum].getElementsByTagName('comments')[0].childNodes[0].nodeValue;

$("#wpmr_fullview > header > h1").text(wpmr_subtitle);
$("#wpmr_fullview > div[data-role='content']").html(wpmr_full);

$(".wpmr_back").text(WpMobiReader_backbutton);
$(".wpmr_comm").attr("href",wpmr_gocomments).text(WpMobiReader_commentbutton);
})

$("#wpmr_nextpage").live("click",function(){   //点了下一页
    if(wpmr_i<x.length){
    wpmr_n++;
    writedown(xml);    
    }
})

$("#wpmr_prepage").live("click",function(){   //点了上一页
    if(wpmr_i>0 && wpmr_i-WpMobiReader_itemsperpage*2>=0){
    wpmr_n--;
    wpmr_i=wpmr_i-WpMobiReader_itemsperpage*2;
    writedown(xml);    }
})

})